<template>
  <!--  <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
    >
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input v-model="ruleForm.pwd" type="password" autocomplete="off"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginFn"
        >登录
        </el-button
        >
      </el-form-item>
    </el-form>-->
  <div class="login-container">
    <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
    >
      <div class="title-container">
        <h3 class="title">xxx电商后台系统</h3>
        <lang-select class="set-language"/>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
        />
      </el-form-item>

      <el-tooltip
          content="Caps lock is On"
          placement="right"
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
          <el-input
              ref="passwordRef"
              v-model="loginForm.password"
              type="password"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter="loginFn"
          />
        </el-form-item>
      </el-tooltip>

      <!-- 验证码 -->
      <!--      <el-form-item prop="code">
              <span class="svg-container">
                <svg-icon icon-class="valid_code" />
              </span>
              <el-input
                  v-model="loginForm.code"
                  auto-complete="off"
                  :placeholder="$t('login.code')"
                  style="width: 65%"
                  @keyup.enter="handleLogin"
              />

              <div class="captcha">
                <img
                    :src="captchaBase64"
                    @click="handleCaptchaGenerate"
                    height="38px"
                />
              </div>
            </el-form-item>-->

      <el-button
          size="default"
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.prevent="loginFn"
      >登录
      </el-button>

      <!-- 账号密码提示 -->
      <div class="tips">
        <div style="position: relative">
          <span style="margin-right: 20px"
          >账号: admin</span
          >
          <span> 密码: 123456</span>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import {reactive, toRefs, ref} from 'vue'
import {adminLoginApi} from '../../request/api'
import Cookie, {get} from 'js-cookie'
import {useRouter} from 'vue-router'

import {useStore} from 'vuex'


const validatePwd = (rule: unknown, value: string | undefined, cb: (mse?: string) => void) => {
  if (!value) {
    cb('密码不能为空')
  } else {
    cb()
  }
}

const state = reactive({
  loginForm: {
    username: 'admin',
    password: '123456'
  },
  loginRules: {
    username: [{required: true, trigger: 'blur'}],
    password: [{required: true, trigger: 'blur', validator: validatePwd}]
  },
  loading: false,
})


let {loginRules, loginForm, loading} = toRefs(state);

// 获取el-form对象
let loginFormRef = ref();

// 获取项目路由对象
let router = useRouter();

// 获取项目vuex
let store = useStore();


const loginFn = () => {
  loginFormRef.value.validate().then(() => {
    console.log("校验通过")
    state.loading = true;
    adminLoginApi(loginForm.value).then(res => {
      if (res.code === 200) {
        // 先存储token
        Cookie.set("token", res.data.tokenHead + res.data.token, {expires: 1})

        // 获取用户信息
        store.dispatch('getAdminInfo').then(res => {
          // 跳转homepage
          router.push("/homepage")
        })
        state.loading = false;
      }
    }).catch(() => {
      state.loading = false;
    })

  }).catch(() => {
    console.log("校验不通过")
  })
}

</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

/* reset element-ui css */
.login-container {
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .el-input {
    display: inline-block;
    height: 36px;
    width: 85%;

    .el-input__wrapper {
      padding: 0;
      background: transparent;
      box-shadow: none;

      .el-input__inner {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        color: $light_gray;
        height: 36px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-input__inner {
    &:hover {
      border-color: var(--el-input-hover-border, var(--el-border-color-hover));
      box-shadow: none;
    }

    box-shadow: none;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .copyright {
    width: 100%;
    position: absolute;
    bottom: 0;
    font-size: 12px;
    text-align: center;
    color: #cccccc;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 5px 10px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .captcha {
    position: absolute;
    right: 0;
    top: 0;

    img {
      height: 42px;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}

.thirdparty-button {
  position: absolute;
  right: 40px;
  bottom: 6px;
}

@media only screen and (max-width: 470px) {
  .thirdparty-button {
    display: none;
  }
}
</style>
